package com.clife.gradle.http.okhttp.okhttp;

import com.clife.gradle.util.Logc;

import okhttp3.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OkHttpApi {
    public static long READ_TIMEOUT = 15 * 1000;
    public static long WRITE_TIMEOUT = 15 * 1000;
    public static long CONNECT_TIMEOUT = 15 * 1000;
    protected InputStream bksFile;
    protected String password;
    protected InputStream[] certificates;

    public void get() throws IOException {
        String ENDPOINT = "https://api.github.com/repos/square/okhttp/contributors";
        OkHttpClient client = new OkHttpClient();
        // Create request for remote resource.
        Request request = new Request.Builder()
                .url(ENDPOINT)
                .build();
        Response response = client.newCall(request).execute();
        ResponseBody body = response.body();
        String string = body.string();
        System.out.println(string);
    }

    public String post(String url, Map<String, String> parameterMap) {
        List<String> parameterList = new ArrayList<>();
        FormBody.Builder builder = new FormBody.Builder();
        if (parameterMap.size() > 0) {
            parameterMap.keySet().forEach(parameterName -> {
                String value = parameterMap.get(parameterName);
                builder.add(parameterName, value);
                parameterList.add(parameterName + ":" + value);
            });
        }

        FormBody formBody = builder.build();

        OkHttpClient client = newOkHttpBuilder().build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();

        try {
            Response response = client.newCall(request).execute();
            int httpCode = response.code();
            if (httpCode == 200 && response.body() != null) {
                String result = response.body().string();
                Logc.i("code:"+result);
                return result;
            }else{
                Logc.i("code:"+httpCode +","+response.body().string());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public String put(String url, Map<String, String> parameterMap) {
        //List<String> parameterList = new ArrayList<>();
        FormBody.Builder builder = new FormBody.Builder();
        if (parameterMap.size() > 0) {
            parameterMap.keySet().forEach(parameterName -> {
                String value = parameterMap.get(parameterName);
                builder.add(parameterName, value);
                //parameterList.add(parameterName + ":" + value);
            });
        }

        FormBody formBody = builder.build();

        OkHttpClient client = newOkHttpBuilder().build();

        Request request = new Request.Builder()
                .url(url)
                .put(formBody)
                .build();

        try {
            Response response = client.newCall(request).execute();
            int httpCode = response.code();
            if (httpCode == 200 && response.body() != null) {
                String result = response.body().string();
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    public String get(String url) {
        OkHttpClient.Builder builder = newOkHttpBuilder();
        OkHttpClient client = builder.build();
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        try {
            Response response = client.newCall(request).execute();

            int httpCode = response.code();
            if (httpCode == 200 && response.body() != null) {
                ResponseBody body = response.body();
                String string = body.string();
                //System.out.println(string);
                return string;
            }else{
                //ResponseBody re = response.body();
                //System.err.println(re.string());
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    public OkHttpClient.Builder newOkHttpBuilder() {
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder()
                .readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
//                .addInterceptor(new HttpLoggingInterceptor("gitee", HttpLoggingInterceptor.Level.BASIC));

        okBuilder.hostnameVerifier(new HttpsUtils.UnSafeHostnameVerifier());
        okBuilder.sslSocketFactory(HttpsUtils.getSslSocketFactory(certificates, bksFile, password));
        return okBuilder;
    }
}
